package com.example.s.Activity.setting;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.s.Activity.BaseActivity;
import com.example.s.R;

public class IdentifyPhone extends BaseActivity {
    private ImageView back;
    private TextView phoneTitle;
    private EditText phoneNum;
    private EditText identifyCode;
    private TextView getIdentifyCode;
    private Handler mHandler;
    private boolean mPhoneNumOk = false;
    private TextView confirm;


    private void initView() {

        back = (ImageView) findViewById(R.id.back);
        phoneTitle = (TextView) findViewById(R.id.phoneTitle);
        phoneNum = (EditText) findViewById(R.id.phoneNum);
        identifyCode = (EditText) findViewById(R.id.identifyCode);
        getIdentifyCode = (TextView) findViewById(R.id.getIdentifyCode);
        confirm = (TextView) findViewById(R.id.confirm);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_identify_phone);
        initView();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        phoneNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                autoAddNull(s, start, before);
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 13) {  //加上空格
                    getIdentifyCode.setVisibility(View.VISIBLE);
                    String resultPhone = s.toString().trim().replace(" ", "");
                    Log.d(TAG, "afterTextChanged: " + resultPhone);
                    mPhoneNumOk = true;
                } else {
                    getIdentifyCode.setVisibility(View.INVISIBLE);
                    mPhoneNumOk = false;

                }
            }
        });
        identifyCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 6 && mPhoneNumOk) { //再次删除电话号码,确定按钮不能变颜色
                    confirm.setBackgroundResource(R.drawable.setting_btn_bg_confirm);
                } else {
                    confirm.setBackgroundResource(R.drawable.setting_btn_bg);
                }
            }
        });
        mHandler = new Handler(Looper.getMainLooper());
        getIdentifyCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mHandler.post(mCodeCountdownTask);
                v.setEnabled(false);
                //发送验证码
            }
        });


    }

    private int mCountdownNum = 60;
    private Runnable mCodeCountdownTask = new Runnable() {
        @Override
        public void run() {
            if (mCountdownNum <= 0) {
                mHandler.removeCallbacks(mCodeCountdownTask);
                getIdentifyCode.setText("获取验证码");
                getIdentifyCode.setEnabled(true);
                mCountdownNum = 60;
                return;
            }
            mCountdownNum--;
            getIdentifyCode.setText(mCountdownNum + "s后重试");
            mHandler.postDelayed(mCodeCountdownTask, 1000);
        }
    };

    private void autoAddNull(CharSequence s, int start, int before) {
        if (s == null || s.length() == 0) {
            return;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (i != 3 && i != 8 && s.charAt(i) == ' ') {
                continue;
            } else {
                stringBuilder.append(s.charAt(i));
                if ((stringBuilder.length() == 4 || stringBuilder.length() == 9)
                        && stringBuilder.charAt(stringBuilder.length() - 1) != ' ') {
                    stringBuilder.insert(stringBuilder.length() - 1, ' ');
                }
            }
        }
        if (!stringBuilder.toString().equals(s.toString())) {
            int index = start + 1;
            if (stringBuilder.charAt(start) == ' ') {
                if (before == 0) {
                    index++;
                } else {
                    index--;
                }
            } else {
                if (before == 1) {
                    index--;
                }
            }
            phoneNum.setText(stringBuilder.toString());
            phoneNum.setSelection(index);
        }
    }


}
